/**
 * @Date 2023-02-21 14:00:07
 * @Remark
 */

// lib
// config
// script & methods & public
// store
import {
  addBookService,
  deleteBookService,
  editBookService,
  getCategoryLongPageService,
  getBookPageService,
  getRoomNoPageService,
  getShelfPageService,
} from "biz/Books/service";
import { state } from "./state";
// service
import { ReqBookAddPage, ReqBookPage, ReqShelfPage } from "biz/Books/api";
import toast from "components/toast";
import logUtils from "utils/logUtils";
import { MODULE_NAME } from "./config";
// interface
// 其它

export async function getBookPage() {
  const sp = state.getSearch();
  const params: ReqBookPage = {
    pageNum: state.page,
    pageSize: state.size,
    ...sp,
  };
  state.loading = true;
  const result = await getBookPageService(params);
  state.loading = false;
  if (result.payload) {
    state.total = result.payload.total;
    state.list = result.payload.list;
  }
}
// 详情
export async function getBookDetail(id: string) {
  // state.loading = true;
  // const result = await getBookDetailPageService({ id });
  // state.loading = false;
  // if (result.payload) {
  //   state.editModal = result.payload;
  // }
}

export async function addBook(form: any) {
  const params: ReqBookAddPage = {
    ...form,
  };
  state.editModalLoading = true;
  const result = await addBookService(params);
  state.editModalLoading = false;
  if (result.payload) {
    toast.success("添加成功");
    state.editModal = false;
    getBookPage();
  }
}

// 图书分类不分页列表
export async function getCategoryLongPage() {
  state.loading = true;
  const result = await getCategoryLongPageService({});
  state.loading = false;
  if (result.payload) {
    state.categoryList = result.payload
      .filter((item: CategoryDto) => item.parentCategoryId !== null)
      // .map((item: CategoryDto) => ({
      //   label: item.categoryName,
      //   value: item.categoryId,
      // }));
  }
}

export async function updateBook(form: any, id: string) {
  // logUtils.info(`${MODULE_NAME}页面表单`, form);
  // 调用服务
  state.editModalLoading = true;
  const result = await editBookService({
    id,
    ...form,
  });
  state.editModalLoading = false;
  // 成功
  if (result.payload) {
    toast.success("编辑成功");
    state.editModal = false;
    getBookPage();
  }
}

export async function changeStatusBook(form: any, id: string) {
  logUtils.info(`${MODULE_NAME}页面表单`, form);
  // 调用服务
  // state.editModalLoading = true;
  // const result = await changeStatusBookService({
  //   id,
  //   ...form,
  // });
  // state.editModalLoading = false;
  // if (result.payload) {
  //   toast.success("修改成功");
  //   getBookPage();
  // }
}

export async function delBook(id: number) {
  const result = await deleteBookService({ bookId: id });
  if (result.payload) {
    toast.success("删除成功");
    getBookPage();
  }
}
export async function getRoomLongPage() {
  // state.loading = true;
  const result = await getRoomNoPageService({});
  // state.loading = false;
  if (result.payload) {
    state.roomList = result.payload;
  }
}

export async function getShelfByName(shelfName:string,setShelfList:(arg0: any)=>void){
  const params: ReqShelfPage = {
    pageNum: state.page,
    pageSize: 20,
    shelfName
  };
  // state.loading = true;
  const result = await getShelfPageService(params);
  // state.loading = false;
  if (result.payload) {
    setShelfList(result.payload.list)
  }
}


export async function didMount() {
  getCategoryLongPage();
  getRoomLongPage()
  getBookPage();
}
